<template>
    <div class="user-password-set">
        <ul class="form-box">
            <li class="form__item detail">
                <span class="form__key">新交易密码</span>
                <div class="form__value">
                    <input v-model='password' type="password" placeholder="请输入6位数字密码">
                </div>
            </li>
            <li class="form__item">
                <span class="form__key">确认交易密码</span>
                <div class="form__value">
                    <input v-model='confirmPassword' type="password" placeholder="请再次输入交易密码">
                </div>
            </li>
        </ul>

        <span class="button long confirm" @click='resetLoginPassword'>确定</span>
    </div>
</template>

<script>
    import { Promise } from 'es6-promise'
    import  { mapGetters, mapActions }  from 'vuex'
    import { passwordSetValidation } from '../validation/validation'
    import { userApi } from '../api/api'
    import moment from 'moment'

    export default {
        name:'setLoginPassword',
        components:{

        },
        data() {
            return {
                password:'',
                confirmPassword:'',
            }
        },
        computed:{
            ...mapGetters([
                'msgTip',
                'localInfo'
            ]),
            transactionPassword(){
                return this.localInfo.transactionPassword
            },
            needLogin(){
                return this.localInfo.needLogin
            }
        },
        methods:{
            // actions
            ...mapActions([
                'showLoading',
                'hideLoading',
                'setMsgTip',
                'setNeedLogin'
            ]),

            // filters
            timestampToDate(timestamp){
                if(!timestamp) return this.placeholder
                return moment(timestamp).format('YYYY-MM-DD')
            },

            // function
            goRouter(name){
                this.$router.push({
                    name:name
                })
            },

            resetLoginPassword(){
                let params
                params = {
                    password:this.password,
                    confirmPassword:this.confirmPassword
                }
                if(!passwordSetValidation(params,'transaction')){
                    return
                }
                this.showLoading()
                userApi.updateTradePassword({
                    oldPassword:this.transactionPassword,
                    newPassword:this.password,
                    confirmPassword:this.confirmPassword,
                })
                .then(result=>{
                    this.setMsgTip({
                        text:"更新交易密码成功!",
                        type:'success'
                    })
                    this.hideLoading()
                    history.go(-2)
                })
                .catch(error => {
                    console.error(error)
                    this.hideLoading()

                })
            }
        },
        created(){
            
        }
    }
</script>

<style lang="scss">
    @import "../sass/variables";
    @import "../sass/extends";
    @import "../sass/mixins";
    .user-password-set{
    }

</style>
